package cn.com.tuns.assess.qsb.frame.widget;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import cn.com.tuns.assess.qsb.R;


public class BottomDialog {

	private Activity activity;

	private View main;

	private DialogUtil dialogUtil;

	private boolean canselAble = false;

	private int select;

	public BottomDialog(Activity activity) {
		this.activity = activity;
		select = -1;
	}

	public BottomDialog createDialog(String message, String[] text,
                                     final OnClickListener[] onClick, boolean showCansel) {
		View view = View.inflate(activity, R.layout.bottom_dialog_main, null);
		TextView textView = (TextView) view.findViewById(R.id.message);
		if (message == null || message.length() == 0)
			textView.setVisibility(View.GONE);
		else
			textView.setText(message);

		View cansel = view.findViewById(R.id.cancel);
		if (showCansel) {
			cansel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		} else {
			cansel.setVisibility(View.GONE);
		}

		LinearLayout add = (LinearLayout) view.findViewById(R.id.add);
		add.removeAllViews();
		if (text != null && text.length > 0) {
			for (int i = 0; i < text.length; i++) {
				View child = View.inflate(activity,
						R.layout.bottom_dialog_item, null);
				textView = (TextView) child.findViewById(R.id.text);
				textView.setText(text[i]);
				if (i == select)
					textView.setTextColor(ContextCompat.getColor(activity,
							R.color.theme));

				textView.setTag(i);
				textView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						int index = (Integer) v.getTag();
						dismiss();
						if (onClick[index] != null)
							onClick[index].onClick(v);

					}
				});
				add.addView(child);
			}
		}

		main = view;
		return this;
	}

	public BottomDialog createDialog(String message, String[] text,
                                     final OnItemClick onClick, boolean showCansel) {
		View view = View.inflate(activity, R.layout.bottom_dialog_main, null);
		TextView textView = (TextView) view.findViewById(R.id.message);
		if (message == null || message.length() == 0)
			textView.setVisibility(View.GONE);
		else
			textView.setText(message);

		View cansel = view.findViewById(R.id.cancel);
		if (showCansel) {
			cansel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
		} else {
			cansel.setVisibility(View.GONE);
		}

		LinearLayout add = (LinearLayout) view.findViewById(R.id.add);
		add.removeAllViews();
		if (text != null && text.length > 0) {
			for (int i = 0; i < text.length; i++) {
				View child = View.inflate(activity,
						R.layout.bottom_dialog_item, null);
				textView = (TextView) child.findViewById(R.id.text);
				textView.setText(text[i]);
				if (i == select)
					textView.setTextColor(ContextCompat.getColor(activity,R.color.theme));
				textView.setTag(i);
				add.addView(child);
				textView.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						dismiss();
						int index = (Integer) v.getTag();
						onClick.onItemClick(v, index);
					}
				});
			}
		}

		main = view;
		return this;
	}

	/**
	 * 设置选中状态
	 * 
	 * @param select
	 */
	public void setSelect(int select) {
		this.select = select;
	}

	public void show(DialogUtil.Type type) {
		initMenu(main,type);
	}

	public void show() {
		initMenu(main, DialogUtil.Type.Bottom);
	}

	// public void show(View root) {
	// initMenu(root, main);
	// showMenu(root, Gravity.BOTTOM);
	// }

	// public void showDown(View root) {
	// initMenu(root, main);
	// showMenu(root, Gravity.BOTTOM);
	// }

	public void dismiss() {
		if (dialogUtil != null)
			dialogUtil.dismiss();
		dialogUtil = null;
	}

	public static interface OnItemClick {
		public void onItemClick(View view, int index);
	}

	/**
	 * 点击外部区域是否可以取消 必须在create之后调用
	 * 
	 * @param flag
	 * @return
	 */
	public BottomDialog setOutsideTouchable(boolean flag) {
		canselAble = flag;
		return this;
	}

	public void initMenu(View show,DialogUtil.Type type) {
		dialogUtil = new DialogUtil(activity, show, type, true);
		dialogUtil.show();
		// dialogUtil = DialogUtil.getDialogUtil(activity, show);
	}

}
